package com.hiof.moraa.server.dao;

import com.hiof.moraa.mapping.TblAthlete;
import com.hiof.moraa.mapping.TblAthleteLiveLocation;
import com.hiof.moraa.mapping.TblSpot;
import com.hiof.moraa.mapping.TblTeam;
import com.hiof.moraa.server.service.model.GoogleGpsGeoPoint;
import com.hiof.moraa.server.service.model.LiveLocationFeed;
import com.hiof.moraa.server.service.model.MeetingPointInfo;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: Bergy
 * Date: 7/7/12
 * Time: 8:55 AM
 * To change this template use File | Settings | File Templates.
 */
public class HibernateSpotDAOImpl implements SpotDAO {

    private SessionFactory factory;

    public SessionFactory getFactory() {
        return factory;
    }

    public void setFactory(SessionFactory factory) {
        this.factory = factory;
    }

    @Override
    public MeetingPointInfo insertMeetingPoint(int teamId, double lat, double lng, double alt) {
        TblSpot spot = new TblSpot();
        try {
            TblTeam team = (TblTeam) factory.getCurrentSession().get(TblTeam.class, teamId);
            spot.setLat(lat);
            spot.setLng(lng);
            spot.setHeight(alt);
            spot.setType(SpotType.H.toString());
            spot.setTblTeam(team);

            factory.getCurrentSession().save(spot);
        } catch (HibernateException e) {
            Logger.getLogger(HibernateSpotDAOImpl.class.getName()).log(Level.SEVERE, "Hibernate Exception Occured", e);
        }

        MeetingPointInfo meetingPointInfo = convert2MeetingPointInfo(spot);
        return meetingPointInfo;
    }

    @Override
    public List<MeetingPointInfo> getListOfMeetingPoint(int teamId) {
        Session session = factory.getCurrentSession();
        String hql = "from TblSpot spot where " +
                "spot.tblTeam.id = :teamId and " +
                "spot.type = :type";

        Query query = session.createQuery(hql);
        query.setInteger("teamId", teamId);
        query.setString("type", SpotType.H.toString());
        List<TblSpot> spots = query.list();

        List<MeetingPointInfo> result = fetchToMeetingPointList(spots);
        spots.clear();
        return result;
    }

    @Override
    public void deleteMeetingPoint(int meetingPointId) {
        Session session = factory.getCurrentSession();
        String hql = "delete from TblSpot spot where " +
                "spot.id = :id";
        Query query = session.createQuery(hql);
        query.setInteger("id", meetingPointId);

        query.executeUpdate();
    }

    private MeetingPointInfo convert2MeetingPointInfo(TblSpot spot) {
        if (spot == null)
            return null;
        MeetingPointInfo meetingPointInfo = new MeetingPointInfo(
                spot.getId(),
                spot.getTblTeam().getId(),
                new GoogleGpsGeoPoint(spot.getLat(), spot.getLng(), spot.getHeight())
        );
        return meetingPointInfo;
    }

    private List<MeetingPointInfo> fetchToMeetingPointList(List<TblSpot> spots) {
        List<MeetingPointInfo> result = new ArrayList<MeetingPointInfo>();
        for(TblSpot spot: spots){
            result.add(convert2MeetingPointInfo(spot));
        }
        return  result;
    }
}
